pimpas.Page = [];
pimpas.Page.Handler = Class.create( );
pimpas.Extends(pimpas.Page.Handler.prototype, pimpas.Object);
pimpas.Extends(pimpas.Page.Handler.prototype, pimpas.HTTPRequest.Listener.prototype );

pimpas.Page.Handler.Component = Class.create( );
pimpas.Extends(pimpas.Page.Handler.Component.prototype, pimpas.Object);
pimpas.Extends(pimpas.Page.Handler.Component.prototype,
	{
		listenerType: null,
		component: null,
		init: function(listenerType, component) {
			this.listenerType 		= listenerType;
			this.component			= component;
		}
	}
);

pimpas.Extends(pimpas.Page.Handler.prototype,
	  {
		  observeFunction: null,
		  init: function( ) {
		  	var obj = this;
			this.observeFunction = function( event ) { obj.observe.bind( obj, event )() };
		  },
		  registerFor: function( type, el ) {
			el = pimpas.DOM.getElement( el );
			var obj = this;
			this.instance = obj;
			var box = new pimpas.Page.Handler.Component();
			box.init(type, el );
			pimpas.Page.Handler.components.push(box);
			
			if(type==pimpas.Page.Handler.MOUSELISTENER) {
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.MOUSEMOVE, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.MOUSEOVER, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.MOUSEOUT, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.MOUSEDOWN, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.MOUSEUP, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.CLICK, 		obj.observeFunction, true );
			} else if(type==pimpas.Page.Handler.KEYLISTENER) {
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.KEYPRESS, 	obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.KEYUP, 		obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.KEYDOWN,		obj.observeFunction, true );
			} else if(type==pimpas.Page.Handler.FOCUSLISTENER) {
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.FOCUS, 		obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.BLUR, 		obj.observeFunction, true );
			} else if(type==pimpas.Page.Handler.LOADUNLOADLISTENER) {
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.LOAD, 		obj.observeFunction, true );
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.UNLOAD, 		obj.observeFunction, true );
			} else if(type==pimpas.Page.Handler.SUBMITLISTENER)
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.SUBMIT, 		obj.observeFunction, true );
		  },
		  observe: function( event ) {
			  var e 		= pimpas.DOM.Event.fixEvent( event );
			  var element 	= pimpas.DOM.Event.getElement( e );
			  
			  this.log.debug("Event Type: "+ e.type+", Event Target: "+element);
			  
			  this.bindToServer( e.type, element, e );
		  },
		  bindToServer: function( eventType, element, event) {
		  	  var box = pimpas.Page.Handler.getComponent( (element.id!=null) ? element.id : element.name );
		  	  
			  var req = new pimpas.HTTPRequest( );
			  req.cleanUp( );
			  req.create( );
			  req.queueMode = false;
			  req.sendForm( element, null );
			  req.async = false;
			  req.addParameter( "bce_framework_execution", "true" );
			  req.addParameter( "component", (element.id) ? element.id : element.name );
			  req.addParameter( "eventType", eventType );
			  
			  var ar = this.process( box, event, eventType );
			  
			  for(var i=0;i<ar.length;i++)
			  	req.addParameter( ar[i][0], ar[i][1] );
			  
			  var obj = this;
			  req.addListener( obj );
			  req.request( );
		  },
		  process: function(box, event, eventType) {
		  	var ar = new Array();
		  	
		  	ar.push(["pageX", event.pageX]);
		  	ar.push(["pageY", event.pageY]);
		  	ar.push(["screenX", event.screenX]);
		  	ar.push(["screenY", event.screenY]);
		  	
		  	if(box.listenerType == pimpas.Page.Handler.MOUSELISTENER) {
				// mouse event
				ar.push(["pointerX", pimpas.DOM.Event.pointerX( event ) ]);
				ar.push(["pointerY", pimpas.DOM.Event.pointerY( event ) ]);
				if(eventType == pimpas.DOM.Event.CLICK.toLowerCase()) {
					ar.push(["isLeftClick", pimpas.DOM.Event.isLeftClick( event ) ]);
					ar.push(["isRightClick", pimpas.DOM.Event.isRightClick( event ) ]);
			  	}
		  	}
		  	 
		  	return ar;
		  },
		  callBack: function(obj, evt, text, xml) {
		  	if(evt == pimpas.HTTPRequest.events.onComplete) {
						  	
		  	}
		  }
	  }
);

pimpas.Extends(pimpas.Page.Handler, 
	{
		instance: null,
		defaultInterval: 300,// miliseconds
		getInstance: function( ) {
			if(this.instance == null) {
				this.instance = new pimpas.Page.Handler( );
				this.instance.init( );
			}
			
			return this.instance;
		},
		components: new Array(),
		getComponent: function(identifier) {
			for(var i=0;i<pimpas.Page.Handler.components.length;i++) {
				if((pimpas.Page.Handler.components[i].component.id != null &&
					pimpas.Page.Handler.components[i].component.id == identifier) ||
					(pimpas.Page.Handler.components[i].component.name != null &&
					pimpas.Page.Handler.components[i].component.name == identifier))
						return pimpas.Page.Handler.components[i];
			}
			return null;
		},
		MOUSELISTENER: 0,
		KEYLISTENER: 1,
		FOCUSLISTENER: 2,
		LOADUNLOADLISTENER: 3,
		SUBMITLISTENER: 4
	}
);